package stack;

import java.util.Arrays;
import java.util.Stack;

/**
 * @author : Rookie
 * @description : 单调栈 每日温度
 * @date : 2023/7/8
 */
public class Temperature {

    public static void main(String[] args) {
        int[] t = {73, 74, 75, 71, 69, 69, 72, 76, 73};
        // 寻找右侧比当前元素大的最近的索引位置的差
        int[] result = getTemperateureChange(t);
        System.out.println(Arrays.toString(result));
    }

    private static int[] getTemperateureChange(int []t) {
        // 记录结果
        int[] result = new int[t.length];
        Stack<Integer> stack = new Stack<>();
        stack.push(0);
        for (int i = 1; i < t.length; i++) {
            if (!stack.isEmpty()) {
                if (t[i] <= t[stack.peek()]) {
                    stack.push(i);
                } else {
                    while (!stack.isEmpty() && t[i] > t[stack.peek()]) {
                        result[stack.peek()] = i - stack.peek();
                        stack.pop();
                    }
                    stack.push(i);
                }
            }
        }
        return result;
    }
}
